//=============================================================================
#include "Projectile.h"
#include <math.h>
#include "SoundBank.h"
#include "Random.h"
#include "AppStateManager.h"
#include "log.h"

//=============================================================================
CProjectile::CProjectile(int X, int Y, int tarX, int tarY, int id) {
	this->X = X;
	this->Y = Y;
	this->targetX = tarX;
	this->targetY = tarY;
	this->Flags = ENTITY_FLAG_NONE;
	this->Type = ENTITY_TYPE_PLAYERSPELL;
	this->collided = false;
	this->exploded = false;
	this->damage = 10;
	this->spellID = id;
	this->oldTime = 0;
	spellIdentifier = CEntity::ProjectileList.size();

	vect.X = (targetX-X);
	vect.Y = (targetY-Y);

	double vectLength = sqrt(((double)vect.X*(double)vect.X) + ((double)vect.Y*(double)vect.Y));

	xSpeed = vect.X/vectLength;
	ySpeed = vect.Y/vectLength;

}

void CProjectile::CastProjectile(int identifier)
{

}

void CProjectile::projectileToXY()
{
	this->OnAnimate();
	collided = OnProjectileMove(X,Y);
	if(!collided)
	{
		X = X+ (xSpeed * CEntity::PlayerList.at(this->playerID)->castTime * CFPS::FPSControl.GetSpeedFactor()); //Gets the players casttime, then sets the x,y for the coordinate.
		Y = Y+ (ySpeed * CEntity::PlayerList.at(this->playerID)->castTime * CFPS::FPSControl.GetSpeedFactor()); 
	}
	else
	{
		if(oldTime == 0)
		{
			if(this->spellID == 4)
			{
				this->Surf_Entity = Surface_FireBallHit;
				this->Width = 30;
				this->Height = 40;
				this->Anim_Control.MaxFrames = 6;
			}
			else if(this->spellID == 2)
			{
				this->Surf_Entity = Surface_FreezeHit;
				this->Width = 30;
				this->Height = 40;
				this->Anim_Control.MaxFrames = 6;
			}
			oldTime = SDL_GetTicks();
		}
		if(SDL_GetTicks() > (this->oldTime+100))
		{
				this->Surf_Entity = NULL;
				this->X = NULL;
				this->Y = NULL;
				this->~CProjectile();
		}
	}
}


//=============================================================================
bool CProjectile::OnLoad(char* File, int Width, int Height, int MaxFrames) {
    if(CEntity::OnLoad(File, Width, Height, MaxFrames) == false) {
        return false;
    }

    return true;
}

//-----------------------------------------------------------------------------
void CProjectile::OnLoop() {
	projectileToXY();
	CEntity::OnLoop();
}

//-----------------------------------------------------------------------------
void CProjectile::OnRender(SDL_Surface* Surf_Display) {
	CEntity::OnRender(Surf_Display,1000);
}

//------------------------------------------------------------------------------
void CProjectile::OnCleanup() {
	CEntity::OnCleanup();
}

//------------------------------------------------------------------------------
void CProjectile::OnAnimate() {
	Anim_Control.MaxFrames = 5;
	CEntity::OnAnimate();
}

//------------------------------------------------------------------------------
bool CProjectile::OnCollision(CEntity* Entity) {
	if(Entity->playerID-2 != this->playerID && Entity->Type == ENTITY_TYPE_PLAYER && CAppStateManager::GetActiveAppStateInt() == 5 && !exploded)
	{
		x.Clear();
		x << 0;
		x << this->spellIdentifier;
		x << Entity->playerID-2;
		x << 9;
		x << 0;
		x << 0;
		x << "test";
		CClient::getInstance().ServerBroadcastPacket(x);
		this->RunCollision(Entity);
		this->exploded = true;
	}
	return true;
}

void CProjectile::RunCollision(CEntity* Entity)
{
	
	if(Entity->Type == ENTITY_TYPE_PLAYER)
		{
			this->X = Entity->X;
			this->Y = Entity->Y;
			this->OnAnimate();
			if(oldTime == 0)
			{
				CSoundBank::SoundControl.setSoundPosition(CCamera::CameraControl.GetX()+(WWIDTH/2), CCamera::CameraControl.GetY()+(WHEIGHT/2), Entity->X, Entity->Y);
				CSoundBank::SoundControl.Play(Random(1,3), 1); //hurt sound-effect
				if(this->spellID == 4)
				{
				
					this->Surf_Entity = Surface_FireBallHit;
					this->Width = 30;
					this->Height = 40;
					this->Anim_Control.MaxFrames = 6;
			
					Entity->Surf_Entity = Surface_PlayerHurt;
					Entity->Width = 22;
					Entity->Height = 31;
					Entity->Anim_Control.MaxFrames = 3;
					oldTime = SDL_GetTicks()+600;
			
				}
				else if(this->spellID == 2)
				{
			
					this->Surf_Entity = Surface_FreezeHit;
					this->Width = 30;
					this->Height = 40;
					this->Anim_Control.MaxFrames = 6;

			
					Entity->Surf_Entity = Surface_PlayerFrozen;
					Entity->Width = 22;
					Entity->Height = 31;
					Entity->Anim_Control.MaxFrames = 3;
					oldTime = SDL_GetTicks()+1000;
				}
				else if(this->spellID == 5)
				{
			
					Entity->Surf_Entity = Surface_PlayerHurt;
					Entity->Width = 22;
					Entity->Height = 31;
					Entity->Anim_Control.MaxFrames = 3;
					oldTime = SDL_GetTicks()+200;
				}
		
			}
			if(SDL_GetTicks() > (this->oldTime))
			{
				Entity->Surf_Entity = Surface_Player;
				Entity->Width = 22;
				Entity->Height = 31;
				Entity->Anim_Control.MaxFrames = 3;
				this->Surf_Entity = NULL;
				this->X = NULL;
				this->Y = NULL;
				this->~CProjectile();
			}
		}
}
//=============================================================================
